//
// Created by pengshuai on 2021/6/24.
//

#include <iostream>
#include <vector>
#include <string>
#include <windows.h>
#include <cinttypes>
#include <conio.h>
#include "tobii_research.h"
#include "tobii_research_eyetracker.h"
#include "tobii_research_streams.h"
using namespace std;

TobiiResearchEyeTracker *eyeTracker;
vector<TobiiResearchGazeData> vt;

TobiiResearchStatus find_all_eyetrackers_example() {
    TobiiResearchEyeTrackers* eyetrackers = nullptr;
    TobiiResearchStatus result;
    size_t i;
    result = tobii_research_find_all_eyetrackers(&eyetrackers);
    if (result != TOBII_RESEARCH_STATUS_OK) {
        printf("Finding trackers failed. Error: %d\n", result);
        return result;
    }
    for (i = 0; i < eyetrackers->count; i++) {
        TobiiResearchEyeTracker* eyetracker = eyetrackers->eyetrackers[i];
        eyeTracker = eyetracker;
        char* address;
        char* serial_number;
        char* device_name;
        tobii_research_get_address(eyetracker, &address);
        tobii_research_get_serial_number(eyetracker, &serial_number);
        tobii_research_get_device_name(eyetracker, &device_name);
        printf("%s\t%s\t%s\n", address, serial_number, device_name);
        tobii_research_free_string(address);
        tobii_research_free_string(serial_number);
        tobii_research_free_string(device_name);
    }
    printf("Found %d Eye Trackers \n", (int)eyetrackers->count);
    tobii_research_free_eyetrackers(eyetrackers);
    return result;
}


void gaze_data_callback(TobiiResearchGazeData* gaze_data, void* user_data) {
    // to Dst, from Src
    memcpy(user_data, gaze_data, sizeof(*gaze_data));
    cout << "88888" << endl;
    vt.emplace_back(*gaze_data);
}

void gaze_data_example(TobiiResearchEyeTracker* eyetracker) {
    TobiiResearchGazeData gaze_data;
    char* serial_number;
    tobii_research_get_serial_number(eyetracker, &serial_number);
    printf("Subscribing to gaze data for eye tracker with serial number %s.\n", serial_number);
    tobii_research_free_string(serial_number);
    TobiiResearchStatus status;

    status = tobii_research_subscribe_to_gaze_data(eyetracker, gaze_data_callback, &gaze_data);
    if (status != TOBII_RESEARCH_STATUS_OK)
        return;

    while (true) {
        if (_kbhit() && _getch() == 0x1b) {
            cout << "Exiting!\n";
            status = tobii_research_unsubscribe_from_gaze_data(eyetracker, gaze_data_callback);

            printf("Unsubscribed from gaze data with status %i.\n", status);
            printf("Last received gaze package:\n");

            printf("Left_eye.gaze_origin.position_in_user_coordinates on display area: (%f, %f, %f)\n",
                   gaze_data.left_eye.gaze_origin.position_in_user_coordinates.x,
                   gaze_data.left_eye.gaze_origin.position_in_user_coordinates.y,
                   gaze_data.left_eye.gaze_origin.position_in_user_coordinates.z);
            printf("Right_eye.gaze_point.position_in_user_coordinates in user coordinates (%f, %f, %f)\n",
                   gaze_data.right_eye.gaze_point.position_in_user_coordinates.x,
                   gaze_data.right_eye.gaze_point.position_in_user_coordinates.y,
                   gaze_data.right_eye.gaze_point.position_in_user_coordinates.z);
            printf("Right_eye.gaze_point.position_on_display_area in user coordinates (%f, %f)\n",
                   gaze_data.right_eye.gaze_point.position_on_display_area.x,
                   gaze_data.right_eye.gaze_point.position_on_display_area.y);
            break;
        }
    }
}

int main()
{
    find_all_eyetrackers_example();
    gaze_data_example(eyeTracker);
    cout << vt.size() << endl;
    return 0;
}

